home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Programmation / Alpha ƒ / Tcl / SystemCode / getVarValue.tcl < prev    next >
Text File  |  1995-12-28  |  6KB  |  195 lines

  1. #############################################################################
  2. #  Report the current value of a global variable, chosen interactively
  3. #  from a list of all active variables.
  4. #
  5. #  If the variable is an array, or its value is too big to fit in an 
  6. #  alertnote, then its contents are listed in a new window, otherwise 
  7. #  the variable's value is displayed in an alertnote.
  8. #
  9. proc getVarValue {} {
  10.     set def [getText [getPos] [selEnd]]
  11.     set var [listpick -p {Which var?} -L $def [lsort -ignore [info globals]]]
  12.     if {![string length $var]} return
  13.     showVarValue $var
  14. }
  15.  
  16. proc showVarValue {var} {
  17.     global $var
  18.     if {![catch {set $var} value]} {
  19.         if {![catch {alertnote "'$var' = $value"}]} {
  20.             return
  21.         } else {
  22.             new -n "* $var *"
  23.             insertText "'$var' = $value"
  24.         }
  25.     } else {
  26.         new -n "* $var *"
  27.         listArray $var
  28.     }
  29.     goto 0
  30. # if 'shrinkWindow' is loaded, call it to trim the output window.
  31.    catch {shrinkWindow 2}
  32.    set win [lindex [winNames -f] 0]
  33.    setWinInfo -w $win dirty 0
  34.    setWinInfo -w $win read-only 1
  35.  
  36. #############################################################################
  37. #  List the name and value of each element of the array $arrName.
  38. #  (Convenient to use as a shell command.)
  39. #
  40. proc listArray {arrName} {
  41.     global $arrName
  42.     set lines {}
  43.     if {![catch {info vars $arrName}]} {
  44.         foreach nm [array names $arrName] {
  45.             set val [expr \$$arrName\($nm\)]
  46.             append lines "\r\"$nm\"\t\{$val\}"
  47.         }
  48.         insertText $lines
  49.     } else {
  50.         alertnote "\"$arrName\" doesn't exist in this context"
  51.     }
  52. }
  53.  
  54. #############################################################################
  55. #  Write out the active definition of the proc $procName.
  56. #  (Convenient to use as a shell command.)
  57. #
  58. proc listProc {procName} {
  59.     set lines {}
  60.     if {![catch {info procs "*$procName*"} procList]} {
  61.         foreach p $procList {
  62.             set pargs [info args $p]
  63.             set arglist {}
  64.             foreach a $pargs {
  65.                 if {[info default $p $a def]} {
  66.                     append arglist " {$a $def}" 
  67.                 } else {
  68.                     append arglist " $a"
  69.                 }
  70.             }
  71.             append lines "\rproc $p {[string trim $arglist]} {"
  72.             append lines [info body $p]
  73.             append lines "}\r"
  74.         }
  75.         insertText $lines
  76.     }
  77. }
  78.  
  79. #############################################################################
  80. # Adjust the dimensions of the current window to match the length (and 
  81. # optionally the width) of the text that it contains.  If shrinkWidth is 
  82. # omitted or set to zero, then only the height of the window is adjusted.
  83. # If it's set to 1, then the width is adjusted to accomodate the widest
  84. # line in the file; if it's set to 2, then the width is set based on only
  85. # the currently displayed lines (moves insertion onto the screen, as a 
  86. # side effect.)
  87.  
  88. proc shrinkWindow {{shrinkWidth 0}} {
  89.     global defHeight defWidth
  90.     # These constants work for 9-pt Monaco type
  91.     set lineht 11
  92.     set htoff 22
  93.     set chwd 6
  94.     set choff 20
  95.     
  96.     set wd [lindex [getGeometry] 2]
  97.     set ht [lindex [getGeometry] 3]
  98.     set top [lindex [getGeometry] 1]
  99.     set left [lindex [getGeometry] 0]
  100.     
  101.     set mxht [expr [lindex [getMainDevice] 3] - $top - 5 -15]
  102.     set mxwd [expr [lindex [getMainDevice] 2] - $left - 5]
  103.     set mnht 120
  104.     set mnwd 200
  105.  
  106.     set htWd [fileHtWd $shrinkWidth]
  107.     set lines [lindex $htWd 0]
  108.     set chars [lindex $htWd 1]
  109.  
  110.     if {$lines <= 1} then {set lines 10}
  111.     
  112.     
  113.     if {$lines > 0} {
  114.         set ht [expr $htoff + ( $lineht * (1 + $lines)) ]
  115.     } elseif {$ht > $defHeight} {
  116.         set ht $defHeight
  117.     }
  118.     
  119.     if {$chars > 0} {
  120.         set wd [expr $choff + ( $chwd * (2 + $chars)) ]
  121.     } elseif {$wd > $defWidth} {
  122.         set wd $defWidth
  123.     }
  124.     
  125.     if {$ht > $mxht} then {set ht $mxht}
  126.     if {$wd > $mxwd} then {set wd $mxwd}
  127.     if {$ht < $mnht} then {set ht $mnht}
  128.     if {$wd < $mnwd} then {set wd $mnwd}
  129.     sizeWin $wd $ht
  130. }
  131.  
  132. #############################################################################
  133. # Return the number of lines and the maximum number of characters in any 
  134. # line of a file.  It would be nice if there was a built-in command to
  135. # do this (i.e., compiled C code) because this is a pretty slow way to
  136. # get the maximum line width.
  137.  
  138. proc fileHtWd {{checkWidth 0}} {
  139.     set text [getText 0 [maxPos]] 
  140.     getWinInfo arr
  141.     set tabw [expr $arr(tabsize) - 1]
  142.     
  143.     set lines [split $text "\r"]
  144.     set nlines [llength $lines]
  145.  
  146.     if {$checkWidth > 1} {
  147.         set lines [eval lrange \$lines [displayedLines]]
  148.     }
  149.     
  150.     set llen 0
  151.     if {$checkWidth > 0} {
  152.         foreach line $lines {
  153.             regsub {                +░.*$} $line {} line
  154.             regsub {    } $line {    } line
  155.             set len [string length $line]
  156.             if {[set ntab [llength [split $line "\t"]]] > 1} {
  157.                 set len [expr $len + $tabw*($ntab-1)]
  158.             }
  159.             if { $len > $llen} {
  160.                 set llen $len
  161.             }
  162.         }
  163.     }
  164. #    alertnote "Text Height : $nlines ; Text Width : $llen "
  165.     return [list $nlines $llen]
  166. }
  167.  
  168. # Report what range of lines are displayed in any window.
  169. # (A side effect is that the insertion point is moved to the 
  170. # top of the window, if it was previously off-screen)
  171. #
  172. proc displayedLines {{window {}}} {
  173.     if {$window == {}} { set window [lindex [winNames -f] 0] }
  174.  
  175.     bringToFront $window
  176.     set oldPos [getPos]
  177.     moveInsertionHere
  178.     set top [getPos]
  179.     set first [lindex [posToRowCol $top] 0]
  180.     moveInsertionHere -last
  181.     set bottom [getPos]
  182.     set last [lindex [posToRowCol $bottom] 0]
  183.  
  184.     if {$oldPos < $top || $oldPos > $bottom} {
  185.         goto $top
  186.     } else {
  187.         goto $oldPos
  188.     }
  189.  
  190.     return [list $first $last]
  191. }
  192.  
  193.  
  194.